package com.rockcent.mall.controller;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.Routes;
import com.rockcent.mall.lib.dto.CustomerDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.CustomerRepository;
import com.rockcent.mall.lib.repository.CustomerRepositoryCustom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zhangjh on 2016/10/14.
 */
@RestController
public class MerchantCustomerController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(MerchantCustomerController.class);

    @Autowired
    private CustomerRepository customerRepository;

    @Autowired
    private BaseUserHelper userHelper;

    /**
     *
     * @param customerDto
     * @param request
     * @return
     */
    @RequestMapping(value = Routes.MERCHANT_CUSTOMER_PAGE, method = RequestMethod.GET)
    public JsonResult searchCustomerPage(CustomerDto customerDto, HttpServletRequest request) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            PageResult pageResult = this.getPage(customerDto.getPageNo(), customerDto.getPageSize());

            PageResult<CustomerDto> result = customerRepository.getCustomerPage(customerDto.getNickName(), userInfoDto.merchant.getId(), userInfoDto.mall.getId(), pageResult);
             result.data.forEach(customer->{
                //总订单数
                Long orderCount = customerRepository.getCustomerOrderCount(customer.getId(), userInfoDto.merchant.getId(), userInfoDto.mall.id, pageResult);
                //总消费产品总数
                 customer.setOrderCount(orderCount);
                Long buycount = customerRepository.getCustomerProductTotal(customer.getId(), userInfoDto.merchant.getId(), userInfoDto.mall.id, pageResult);
                 customer.setBuyCount(buycount);
                //总核销产品数
                Long verificationTotal = customerRepository.getCustomerProductVerificationTotal(customer.getId(), userInfoDto.merchant.getId(), userInfoDto.mall.id, pageResult);
                //订单消费总额
                 customerDto.setRemainCount(verificationTotal);
                Double amount = customerRepository.getCustomerOrderTotal(customer.getId(), userInfoDto.merchant.getId(), userInfoDto.mall.id, pageResult);
                 customerDto.setAmount(amount);
                //已退产品数
                Long productBack  = customerRepository.getCustomerProductBack(customer.getId(), userInfoDto.merchant.getId(), userInfoDto.mall.id, pageResult);
                 customerDto.setProductBack(productBack);
             });
            jsonResult.data=result;


            result.setUrl(Routes.MERCHANT_CUSTOMER_PAGE);
        };
        return doing.go(request, log);
    }
}
